package com.ihr360.payroll.model.dto.salary.convertor;

import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.entity.staff.CompanySalaryProfileEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileFieldEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileItemsWrapperEntity;
import com.ihr360.payroll.model.dto.salary.profile.CompanySalaryProfile;
import com.ihr360.payroll.model.dto.staff.embeddable.SalaryProfile;
import com.ihr360.payroll.model.dto.staff.embeddable.SalaryProfileItemsWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author richey
 */
@Component
public class SalaryProfileConvertor implements IConvertor<SalaryProfileEntity,SalaryProfile> {


    @Autowired
    private SalaryProfileFieldConvertor salaryProfileFieldConvertor;

    @Override
    public SalaryProfile convertEntityToDto(SalaryProfileEntity entity) {
        return null;
    }

    @Override
    public SalaryProfileEntity convertDtoToEntity(SalaryProfile dto) {

        if (dto == null) {
            return null;
        }
        SalaryProfileEntity entity = new SalaryProfileEntity();
        BeanUtils.copyProperties(dto, entity);
        CompanySalaryProfile companySalaryProfile = dto.getCompanySalaryProfile();
        if (companySalaryProfile != null) {
            CompanySalaryProfileEntity companySalaryProfileEntity = new CompanySalaryProfileEntity();
            BeanUtils.copyProperties(companySalaryProfile,companySalaryProfileEntity);
            List<SalaryProfileFieldEntity> salaryProfileFieldEntities =  salaryProfileFieldConvertor.converDtosToEntities(companySalaryProfile.getProfileFields());
            companySalaryProfileEntity.setProfileFields(salaryProfileFieldEntities);
            entity.setCompanySalaryProfile(companySalaryProfileEntity);
        }
        SalaryProfileItemsWrapper wrapper = dto.getWrapper();
        if (wrapper != null) {
            SalaryProfileItemsWrapperEntity wrapperEntity = new SalaryProfileItemsWrapperEntity();
            BeanUtils.copyProperties(wrapper, wrapperEntity);
            entity.setWrapper(wrapperEntity);
        }

        return entity;
    }
}
